package edu.xhu.utils.thread;

import com.alibaba.ttl.TransmittableThreadLocal;
import edu.xhu.model.biliuser.pojos.BiliUser;

import java.util.HashMap;
import java.util.Map;
import java.util.Stack;

public class BiliUserThreadLocalUtil {
    private final static ThreadLocal<BiliUser> BILI_USER_TTL = new TransmittableThreadLocal<>();

    //存入线程中
    public static void setUser(BiliUser biliUser) {
        BILI_USER_TTL.set(biliUser);
    }

    //线程中获取
    public static BiliUser getUser() {
        return BILI_USER_TTL.get();
    }

    //清理
    public static void clear() {
        BILI_USER_TTL.remove();
    }

    public static String decodeString(String s) {
        char[] charArr = s.toCharArray();
        Stack<Character> stack = new Stack<>();
        StringBuilder temp = new StringBuilder();
        StringBuilder res = new StringBuilder();
        for (char c : charArr) {
            if (c == '[') {
                continue;
            }
            if (c == ']') {
                while (!stack.isEmpty()) {
                    char sc = stack.pop();
                    // 如果是字符,加入到sb中
                    if (sc - '0' >= 27 && sc - '0' <= 54) {
                        temp.insert(0, String.valueOf(sc));
                    } else {
                        // 只能是数字,反转一下,打印n次
                        int n = sc - '0';
                        StringBuilder nSb = new StringBuilder();
                        for (int i = 0; i < n; i++) {
                            nSb.append(temp.toString());
                        }
                        temp = new StringBuilder(nSb.toString());
                    }
                }
                res.append(temp);
                temp = new StringBuilder();
                continue;
            }
            stack.push(c);
        }
        StringBuilder last = new StringBuilder();
        while (!stack.isEmpty()) {
            // 逆序
            last.append(String.valueOf(stack.pop()));
        }
        res.append(last.reverse().toString());
        return res.toString();
    }

    public static class Node {
        public int num;
        public int index;
        public int preLen;

        public Node(int num, int index, int preLen) {
            this.num = num;
            this.index = index;
            this.preLen = preLen;
        }
    }

    public static int[] dailyTemperatures(int[] temperatures) {
        int[] next = new int[temperatures.length];
        Stack<Node> stack = new Stack<>();
        int cur = 0; // 起始位置
        for (int temperature : temperatures) {
            if (stack.isEmpty()) {
                stack.push(new Node(temperature, cur, 0));
            } else {
                int step = 1;
                while (!stack.isEmpty()) {
                    if (temperature < stack.peek().num) {
                        // 直接入栈
                        stack.peek().preLen = step-1;
                        stack.push(new Node(temperature, cur, 0));
                        break;
                    } else {
                        // 说明当前元素比栈顶气温高,栈顶出栈,设置值
                        Node pre = stack.pop();
                        next[pre.index] = step + pre.preLen;
                    }
                    step++;
                }
                if (stack.isEmpty()) {
                    stack.push(new Node(temperature, cur, 0));
                }
            }
            cur++;
        }
        return next;
    }

    public static void main(String[] args) {
        dailyTemperatures(new int[]{73,74,75,71,69,72,76,73});
    }
}
